<?php 


function ExcelOut($res)
{
	Vendor('PHPExcel.Classes.PHPExcel');
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();
	// Set document properties
	$objPHPExcel->getProperties()->setCreator("学并思")
								 ->setLastModifiedBy("最后修改老王")
								 ->setTitle("这个可以有")
								 ->setSubject("这个XX")
								 ->setDescription("老王的秘密")
								 ->setKeywords("老王")
								 ->setCategory("分类");


	// Add some data

	$rowNum = count($res);

	$keyArr = array_keys(current($res));

	$abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	foreach ($keyArr as $k => $v) {
		$objPHPExcel->setActiveSheetIndex(0)->setCellValue($abc[$k].'1', $v);
	}

	foreach ($res as $k => $v) {
		for ($i=0; $i < count($v); $i++) { 
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($abc[$i].($k+2),$v[$keyArr[$i]]);
			// echo $abc[$i].($k+2)."<br>";
			// echo $v[$keyArr[$i]]."<br>";
		}
	}

	// exit;



	// for ($i=1; $i <= $rowNum; $i++) { 
	// 	$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $res[$i]['username']);
	// 	echo $res[$i-1]['username'];
	// }

	// Rename worksheet
	$objPHPExcel->getActiveSheet()->setTitle('xbs');


	// Set active sheet index to the first sheet, so Excel opens this as the first sheet
	$objPHPExcel->setActiveSheetIndex(0);


	// Redirect output to a client’s web browser (Excel5)
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="01simple.xls"');
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');

	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0

	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
	exit;

}




 ?>